/*
 * Copyright(c) 2020-2100 万达信息股份有限公司 版权所有
 * WONDERS INFORMATION CO., LTD. ALL RIGHTS RESERVED.
 */

package com.wondersgroup.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: huym
 * @version: 1.0
 * @date: 2020/11/24
 */
@RestController
public class TestController {
    @GetMapping("/test/test")
    @SentinelResource(value = "aa", fallback = "testFallBack", blockHandler = "testBlockHandler")
    public String test(int id){
        if (id<0){
            throw new RuntimeException("非法参数ID");
        }
        return "test/test";
    }

    public String testBlockHandler(int id, BlockException blockException){
        if (blockException instanceof FlowException){
            return "活动异常火爆，当前请求被限流！！！"+blockException.getClass().getSimpleName();
        }
        if (blockException instanceof DegradeException){
            return "当前服务已被降级处理，请稍后再试！！！"+blockException.getClass().getSimpleName();
        }
        return "当前服务不可用";
    }

    public String testFallBack(int id){
        return id+"：为非法参数，请检查后再重试!!!";
    }

    @GetMapping("/test/test1")
    public String test1(){
        return "test/test1";
    }
}
